
#ifndef __uart_rx_interrupt_c__
#define __uart_rx_interrupt_c__

#include <zneo.h>

#include "uart_rx_interrupt.h"
#include "clock.h"

// Some symbols to make our code easier to read
#define PORTA_UART_RXD 0x10
#define PORTA_UART_TXD 0x20

#define UART_RXD_EN    0x40
#define UART_TXD_EN    0x80

#define UART_RDIRQ_OFF 0x02
#define UART_RDIRQ_EN  0x00
#define UART_MPEN      0x40

#define IRQ0_U0REN     0x10
#define IRQ0_U0TEN     0x08

#define IRQ0_U0RX_CLR  0x10
#define IRQ0_U0TX_CLR  0x08


void init_uart(void) {

	// Write to the LIN-UART baud rate high and low byte registers to set the appropriate
	// baud rate.
	U0BR = SYS_CLK_FREQ/((unsigned long)BAUD*16UL);

	// Enable the LIN-UART pin functions by configuring the associated GPIO port pins fro
	// alternate function operation.
	PAAF |= PORTA_UART_TXD | PORTA_UART_RXD;	

	// Execute a DI instruction to disable interupts.
	DI();

	// Write to the interupt control registers to enable the LIN-UART receiver input and
	// set the appropriate priority.
	IRQ0ENH |= IRQ0_U0REN;
	IRQ0ENL |= IRQ0_U0REN;

	// Clear the LIN-UART receiver interupt int he applicable interupt request register.
	IRQ0SET |= IRQ0_U0RX_CLR;

	// Write to the LIN-UART control 1 register to enable MULTIPROCESSOR (9-bit)
	// mode functions
	U0CTL1 = UART_RDIRQ_EN;

	// Write to the LIN-UART Control 0 register
	U0CTL0 = UART_RXD_EN;

	// Execute an EI instruction to enable interrupts
	EI();
}

#endif